/** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
**/

// this file contains functions and global variables used for controlling 
// the WebTV player component
var video = null;
var ad = null;
var nextItem = null;
var playerConn = "unknown";
	
var videoIsEpisode = false;

function onFlashReady(){
	doPlay();
}

function isUrlPlayable(url) {
	for (var i=0; i<SUPPORTED_PROTOCOLS.length; ++i) {
		if (url.toLowerCase().indexOf(SUPPORTED_PROTOCOLS[i]) == 0) {
			return true;
		}		
	}
	return false;
}

function playVideo(item, isEpisode){
	videoIsEpisode = isEpisode;
	
	video = (item.videos && item.videos.length) ? item.videos : null;
	ad = item.ad;
	nextItem = item.next;
}

function playNextVideo(){
	if(!nextItem) return;
	
	video = (nextItem.videos && nextItem.videos.length) ? nextItem.videos : null;
	ad = nextItem.ad;
	nextItem = nextItem.next;
	
	setTimeout(function () { 
		gotoScreen("video_container");
		setTimeout(function () { 
			gotoScreen("screen_flash"); 
		}, 0);
	}, 0);
}

function doPlay() {
	if (NATIVE_PLAYER_FILE_EXTENSIONS != undefined) {		
		
		var videoURL;
		var singleVideo = video;
		
		if (video[0] != undefined) {
			singleVideo = video[0];
		}
		
		console.debug(singleVideo);
		
		videoURL = singleVideo.link;
		
		if (videoURL) {
			for (var i = 0; i < NATIVE_PLAYER_FILE_EXTENSIONS.length; i++) {
				var testEnd = "." + NATIVE_PLAYER_FILE_EXTENSIONS[i];
				
				if (videoURL.substr(videoURL.length - testEnd.length, testEnd.length) == testEnd) {
					playWithNativePlayer(videoURL);
					return;
				}
			}
		}
	}
	
	setTimeout(function(){
    updateNetworkStatus();
		var swfObject = document.getElementById("swfObject");
		swfObject.setTranslations( _escapeStringsForFL( [str_player_replay, (videoIsEpisode ? str_player_next_episode : str_player_next_movie), str_player_exit] ) );
		swfObject.setDebug(DEBUG);
		if(SERVER_SEEKING_AVAILABLE != null){
			swfObject.setServerSeekingAvailable(SERVER_SEEKING_AVAILABLE);
		}
		swfObject.playVideo(video, ad, (nextItem != null), DYNAMIC_STREAMING_AUTO, playerConn, SERVER_LOGIN);
		swfObject.focus();
	}, 350);
}

function playWithNativePlayer(videoURL) {
	widget.openURL(videoURL);
	onFlashBack();
}

//	It seems, that Flash externalinterface does not transfer Unicode strings well. 
//	Solution: encodeURIComponent On Javascript side and unescape in Actionscript.
function _escapeStringsForFL( arr ) {
	var escapedArr = [];
	for (var i = 0; i < arr.length; ++i) {
		escapedArr.push( encodeURIComponent(arr[i]) );
	}
	return escapedArr;
}

function onStop(){
	setTimeout(function() { gotoScreen('video_container'); }, 350 );
}


function onPlayerError(msg) {
	setTimeout(function() { 
		gotoScreen('video_container');
		Alert.show(str_error);// + msg); 
	}, 350 );
}

function onFlashBack() {
	setTimeout(function() { onStop(); }, 0 );
}

function updateConnectionMode(mode) {
	logDebug("Calling player to updateConnectionMode: " + mode);
	var sfwObject = document.getElementById("swfObject");
	if (sfwObject && sfwObject.updateConnectionMode)
		sfwObject.updateConnectionMode(mode);
}

function pause() {
	if (currentScreen && currentScreen.attr("id") == "screen_flash")
		document.getElementById("swfObject").pause();
}